package com.tsing.service.impl;

import com.tsing.mapper.UserMapper;
import com.tsing.mapper.impl.UserMapperImpl;
import com.tsing.pojo.entity.AdUser;
import com.tsing.service.UserService;

import java.security.SecureRandom;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Base64;

/**
 * @author 吴阳基
 * @datetime 2023/12/1 10:47
 */
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper = new UserMapperImpl();

    @Override
    public AdUser getById(Long userId) throws SQLException {
        return userMapper.getById(userId);
    }


    @Override
    public void add(AdUser adUser) throws SQLException {
        // Generating a random salt for password encryption
        SecureRandom sr = new SecureRandom();
        byte[] salt = new byte[16];
        sr.nextBytes(salt);
        String saltString = Base64.getEncoder().encodeToString(salt);
        adUser.setSalt(saltString);

        adUser.setStatus(0);
        adUser.setLoginTime(LocalDateTime.now());
        adUser.setCreatedTime(LocalDateTime.now());

        userMapper.add(adUser);
    }
}
